<?php

namespace addons\zzuser\controller\api;

use app\common\controller\Api;
use app\common\library\Sms as Service;
use fast\Random;
use think\Cache;
use think\Validate;

class Sms extends Api
{
    protected $noNeedLogin = 'index';
    protected $noNeedRight = 'index';

    public function index()
    {
        $validate = new Validate();
        if (!$validate->check(input(), [
            'mobile' => 'require|regex:1\d{10}',
        ])) {
            $this->error($validate->getError());
        }
        $mobile = input('mobile');
        $sms    = Random::numeric();
        Cache::set($mobile, $sms, 600);
        Service::send($mobile, $sms);
        $this->success();
    }
}
